package com.quick.springbootquicky.service.impl;

import com.quick.springbootquicky.mapper.TestUserMapper;
import com.quick.springbootquicky.pojo.TestUser;
import com.quick.springbootquicky.service.TestUserService;
import com.quick.springbootquicky.utils.MD5Util;
import com.quick.springbootquicky.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.Map;

@Service
public class TestUserServiceImpl implements TestUserService {

    @Autowired
    private TestUserMapper testUserMapper;
    @Override
    public TestUser findByUserName(String username) {
        return testUserMapper.findByUserName(username);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int add(String username, String password) {
        password = MD5Util.encryptMD5(password);
        testUserMapper.add(username,password);
        return 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateUser(TestUser user) {
        user.setUpdateTime(new Date());
        testUserMapper.updateUser(user);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateAvatar(String avatarUrl) {
        // 获取当前登入者的Id
        Map<String,Object> map = ThreadLocalUtil.get();//从当前线程中获取,在拦截器中清除
        Integer id = (Integer) map.get("id");
        testUserMapper.updateAvatar(avatarUrl,id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updatePwd(String password) {
        // 获取当前登入者的Id
        Map<String,Object> map = ThreadLocalUtil.get();//从当前线程中获取,在拦截器中清除
        Integer id = (Integer) map.get("id");
        testUserMapper.updatePwd(password,id);
    }
}
